{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Using truncated SVD to reduce dimensionality"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "iris = load_iris()\n",
    "iris_X = iris.data\n",
    "y = iris.target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.decomposition import TruncatedSVD\n",
    "svd = TruncatedSVD(2)\n",
    "iris_transformed = svd.fit_transform(iris_X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.53028106,  0.44685765])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "svd.explained_variance_ratio_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [1, 3],\n",
       "       [1, 4]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy.linalg import svd\n",
    "import numpy as np\n",
    "D = np.array([[1, 2], [1, 3], [1, 4]])\n",
    "D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((3L, 2L), (2L,), (2L, 2L))"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "U, S, V = svd(D, full_matrices=False)\n",
    "U.shape, S.shape, V.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  2.],\n",
       "       [ 1.,  3.],\n",
       "       [ 1.,  4.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(U.dot(np.diag(S)), V)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  5.91220352e+00,   2.30344211e+00],\n",
       "       [  5.57207573e+00,   1.97383104e+00],\n",
       "       [  5.44648470e+00,   2.09653267e+00],\n",
       "       [  5.43601924e+00,   1.87168085e+00],\n",
       "       [  5.87506555e+00,   2.32934799e+00],\n",
       "       [  6.47699043e+00,   2.32552598e+00],\n",
       "       [  5.51542859e+00,   2.07156181e+00],\n",
       "       [  5.85042297e+00,   2.14948016e+00],\n",
       "       [  5.15851287e+00,   1.77642658e+00],\n",
       "       [  5.64458172e+00,   1.99190598e+00],\n",
       "       [  6.26482670e+00,   2.42727947e+00],\n",
       "       [  5.75150446e+00,   2.02142409e+00],\n",
       "       [  5.48017099e+00,   1.97972378e+00],\n",
       "       [  4.95064168e+00,   2.04947793e+00],\n",
       "       [  6.52528515e+00,   2.91774840e+00],\n",
       "       [  6.78960484e+00,   2.82549990e+00],\n",
       "       [  6.27173006e+00,   2.60908548e+00],\n",
       "       [  5.92899145e+00,   2.26896626e+00],\n",
       "       [  6.64757408e+00,   2.39126214e+00],\n",
       "       [  6.09424306e+00,   2.36154331e+00],\n",
       "       [  6.25352038e+00,   2.12203279e+00],\n",
       "       [  6.07305216e+00,   2.27257849e+00],\n",
       "       [  5.36933796e+00,   2.49857510e+00],\n",
       "       [  6.04055493e+00,   1.87836700e+00],\n",
       "       [  5.90544974e+00,   1.80875447e+00],\n",
       "       [  5.74982272e+00,   1.86063438e+00],\n",
       "       [  5.93531393e+00,   2.00963860e+00],\n",
       "       [  6.03863542e+00,   2.26113533e+00],\n",
       "       [  5.94934149e+00,   2.27753623e+00],\n",
       "       [  5.60042998e+00,   1.88386305e+00],\n",
       "       [  5.63756795e+00,   1.85795717e+00],\n",
       "       [  6.18446606e+00,   2.19486085e+00],\n",
       "       [  6.24972051e+00,   2.62254502e+00],\n",
       "       [  6.47852260e+00,   2.79919732e+00],\n",
       "       [  5.64458172e+00,   1.99190598e+00],\n",
       "       [  5.62052002e+00,   2.25317183e+00],\n",
       "       [  6.16135565e+00,   2.48866436e+00],\n",
       "       [  5.64458172e+00,   1.99190598e+00],\n",
       "       [  5.14517661e+00,   1.90180543e+00],\n",
       "       [  5.92553978e+00,   2.17806326e+00],\n",
       "       [  5.80255956e+00,   2.31127304e+00],\n",
       "       [  4.97122949e+00,   1.51448985e+00],\n",
       "       [  5.22113428e+00,   2.01078338e+00],\n",
       "       [  6.00686864e+00,   1.99517588e+00],\n",
       "       [  6.31629137e+00,   2.04350797e+00],\n",
       "       [  5.51374686e+00,   1.91077210e+00],\n",
       "       [  6.12877022e+00,   2.32512928e+00],\n",
       "       [  5.42268299e+00,   1.99705970e+00],\n",
       "       [  6.18970990e+00,   2.39869637e+00],\n",
       "       [  5.76112904e+00,   2.16588106e+00],\n",
       "       [  9.12033960e+00,  -7.00219921e-02],\n",
       "       [  8.58379652e+00,  -1.34216664e-01],\n",
       "       [  9.12666208e+00,  -3.29349657e-01],\n",
       "       [  7.27578441e+00,  -4.58464245e-01],\n",
       "       [  8.55831307e+00,  -3.94479344e-01],\n",
       "       [  7.87248767e+00,  -4.83302548e-01],\n",
       "       [  8.66607667e+00,  -2.84566378e-01],\n",
       "       [  6.45349296e+00,   2.41828100e-02],\n",
       "       [  8.63783285e+00,  -2.42455583e-01],\n",
       "       [  7.16782218e+00,  -2.89843601e-01],\n",
       "       [  6.47932460e+00,  -3.06969745e-01],\n",
       "       [  7.97830954e+00,  -1.73440471e-01],\n",
       "       [  7.56302580e+00,  -2.66610207e-01],\n",
       "       [  8.33035185e+00,  -4.90736782e-01],\n",
       "       [  7.37351386e+00,   1.80612202e-01],\n",
       "       [  8.70306507e+00,   2.40936792e-03],\n",
       "       [  7.90690441e+00,  -4.71859382e-01],\n",
       "       [  7.65400146e+00,  -1.22221396e-01],\n",
       "       [  8.05377454e+00,  -7.36272611e-01],\n",
       "       [  7.34196793e+00,  -1.81061634e-01],\n",
       "       [  8.41252158e+00,  -5.93229301e-01],\n",
       "       [  7.91637942e+00,  -1.45207918e-02],\n",
       "       [  8.44808823e+00,  -8.27782086e-01],\n",
       "       [  8.25879715e+00,  -4.76274069e-01],\n",
       "       [  8.33365396e+00,  -8.69521519e-02],\n",
       "       [  8.58996943e+00,  -8.06627036e-02],\n",
       "       [  8.86950574e+00,  -4.16033961e-01],\n",
       "       [  9.02334060e+00,  -5.80846389e-01],\n",
       "       [  8.16939279e+00,  -4.12015974e-01],\n",
       "       [  7.23301526e+00,   2.20045780e-01],\n",
       "       [  7.17755719e+00,  -1.93243831e-01],\n",
       "       [  7.10945416e+00,  -8.78781122e-02],\n",
       "       [  7.58494714e+00,  -4.93933360e-02],\n",
       "       [  8.41811361e+00,  -9.80809017e-01],\n",
       "       [  7.75667080e+00,  -5.29025574e-01],\n",
       "       [  8.37607491e+00,  -1.74046941e-01],\n",
       "       [  8.87379829e+00,  -2.44736100e-01],\n",
       "       [  8.08197922e+00,  -5.13358976e-01],\n",
       "       [  7.66806816e+00,  -1.19348195e-01],\n",
       "       [  7.35174208e+00,  -3.49486294e-01],\n",
       "       [  7.57819336e+00,  -5.44080972e-01],\n",
       "       [  8.31701559e+00,  -3.65357932e-01],\n",
       "       [  7.59828340e+00,  -1.74772186e-01],\n",
       "       [  6.49063093e+00,  -1.72306959e-03],\n",
       "       [  7.60544675e+00,  -3.53704996e-01],\n",
       "       [  7.77771213e+00,  -1.27179129e-01],\n",
       "       [  7.75652123e+00,  -2.16143949e-01],\n",
       "       [  8.18342035e+00,  -1.44118344e-01],\n",
       "       [  6.50454806e+00,   3.14031756e-01],\n",
       "       [  7.66722730e+00,  -1.99743050e-01],\n",
       "       [  9.48426430e+00,  -1.51641735e+00],\n",
       "       [  8.31824380e+00,  -1.14140274e+00],\n",
       "       [  9.85279540e+00,  -1.24242625e+00],\n",
       "       [  9.00957304e+00,  -1.20948284e+00],\n",
       "       [  9.36756741e+00,  -1.37751080e+00],\n",
       "       [  1.05875851e+01,  -1.59573990e+00],\n",
       "       [  7.22476846e+00,  -1.01333762e+00],\n",
       "       [  1.01199467e+01,  -1.41988100e+00],\n",
       "       [  9.26075510e+00,  -1.45488611e+00],\n",
       "       [  1.03255671e+01,  -1.16659243e+00],\n",
       "       [  9.05074356e+00,  -7.03352038e-01],\n",
       "       [  8.87157482e+00,  -1.11168392e+00],\n",
       "       [  9.42218461e+00,  -1.04461604e+00],\n",
       "       [  8.13264217e+00,  -1.24254976e+00],\n",
       "       [  8.44016231e+00,  -1.25929299e+00],\n",
       "       [  9.12862074e+00,  -9.77142417e-01],\n",
       "       [  9.14647039e+00,  -1.02693780e+00],\n",
       "       [  1.10346356e+01,  -1.23661071e+00],\n",
       "       [  1.06983077e+01,  -2.06673401e+00],\n",
       "       [  8.16011640e+00,  -1.14788818e+00],\n",
       "       [  9.70946514e+00,  -1.11778643e+00],\n",
       "       [  8.12014678e+00,  -1.03677605e+00],\n",
       "       [  1.06212714e+01,  -1.71254878e+00],\n",
       "       [  8.57440971e+00,  -8.22231669e-01],\n",
       "       [  9.56363450e+00,  -1.05151196e+00],\n",
       "       [  1.00048212e+01,  -1.07232755e+00],\n",
       "       [  8.48595665e+00,  -7.25435915e-01],\n",
       "       [  8.53811261e+00,  -7.15930934e-01],\n",
       "       [  9.09707481e+00,  -1.33881625e+00],\n",
       "       [  9.79265744e+00,  -9.70574060e-01],\n",
       "       [  1.00712425e+01,  -1.33848298e+00],\n",
       "       [  1.09973481e+01,  -8.97823210e-01],\n",
       "       [  9.11386274e+00,  -1.37329210e+00],\n",
       "       [  8.66465493e+00,  -8.06094909e-01],\n",
       "       [  8.67825118e+00,  -1.29221258e+00],\n",
       "       [  1.04397023e+01,  -1.28165912e+00],\n",
       "       [  9.30019482e+00,  -1.14389303e+00],\n",
       "       [  9.10933242e+00,  -1.00103192e+00],\n",
       "       [  8.41168071e+00,  -6.73624156e-01],\n",
       "       [  9.48396516e+00,  -8.90654098e-01],\n",
       "       [  9.48672553e+00,  -1.19302757e+00],\n",
       "       [  9.36359574e+00,  -7.46936164e-01],\n",
       "       [  8.31824380e+00,  -1.14140274e+00],\n",
       "       [  9.73697852e+00,  -1.28814928e+00],\n",
       "       [  9.63078623e+00,  -1.18941534e+00],\n",
       "       [  9.22669839e+00,  -9.29481206e-01],\n",
       "       [  8.56655506e+00,  -1.03657534e+00],\n",
       "       [  9.02610098e+00,  -8.83219863e-01],\n",
       "       [  9.10565990e+00,  -9.96220532e-01],\n",
       "       [  8.49050919e+00,  -9.14876875e-01]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_S = S[0]\n",
    "new_U = U[:, 0]\n",
    "new_U.dot(new_S)\n",
    "\n",
    "\n",
    "tsvd = TruncatedSVD(2)\n",
    "tsvd.fit(iris_X)\n",
    "tsvd.transform(iris_X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
